package com.springboot.swagger2;

import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static com.google.common.base.Predicates.*;

/**
 * @author liuwei
 * @version 2017/7/16 上午12:35
 * @modified TODO
 * @description
 * @see
 * @since 1.0
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createUserApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("user api")
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.springboot.swagger2"))
                .paths(this.userPaths())
                .build();
    }

    private Predicate<String> userPaths(){
        return or(PathSelectors.regex("/api/user.*"));
    }

    @Bean
    public Docket createOrderApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("order api")
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.springboot.swagger2"))
                .paths(this.orderPaths())
                .build();
    }

    private Predicate<String> orderPaths(){
        return or(PathSelectors.regex("/api/order.*"));
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("使用swagger2生成rest api")
                .description("使用swagger2生成rest api")
                .version("1.0")
                .build();
    }
}
